package com.finanzaspersonales;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import clases.Periodo;
import clases.Rubro;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class Calendario_Pagos extends Activity {
	private GridView gridView;
	private TableLayout tl;
	private String idUsuario;
	private String nombre;
	private String auxmonto;
	private int cuota;
	private int idEF;
	private double monto;
	private ArrayList<Periodo> calendario = new ArrayList<Periodo>();
	
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		idUsuario= this.getIntent().getExtras().getString("idUsuario");
		nombre= this.getIntent().getExtras().getString("nombreEF");
		idEF= this.getIntent().getExtras().getInt("idEF");
		cuota= this.getIntent().getExtras().getInt("cuota");
		auxmonto = this.getIntent().getExtras().getString("monto");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calendario_pagos);
		
		EditText etEFCP = (EditText)findViewById(R.id.etEFCP);
        etEFCP.setText(nombre);
        EditText etCuotaEFCP= (EditText)findViewById(R.id.etCuotasCP);
        String auxInt1 = cuota + "";
        etCuotaEFCP.setText(auxInt1);
        EditText etMontoCP= (EditText)findViewById(R.id.etMontoCP);
        etMontoCP.setText(auxmonto);
        
        calendario = llenarCalendario();
        
		tl = (TableLayout)findViewById(R.id.Tabla);
		loadTableLayout();
        /* Create a new row to be added. */
//        TableRow tr = new TableRow(this);
//        tr.setLayoutParams(new LayoutParams(
//                       LayoutParams.FILL_PARENT,
//                       LayoutParams.WRAP_CONTENT));
//             /* Create a Button to be the row-content. */
//             TextView text = new TextView(this);
//             text.setText("Dynamic Button");
//             text.setLayoutParams(new LayoutParams(
//                       LayoutParams.FILL_PARENT,
//                       LayoutParams.WRAP_CONTENT));
//             /* Add Button to row. */
//             tr.addView(text);
//             /* Add row to TableLayout. */
//             tl.addView(tr,new TableLayout.LayoutParams(
//             LayoutParams.FILL_PARENT,
//             LayoutParams.WRAP_CONTENT));
	}
	
	private ArrayList<Periodo> llenarCalendario() {
		ArrayList<Periodo> calendario = new ArrayList<Periodo>();
		JSONArray  jArray = null;
		try { 
			jArray = (new JSONParser_Final().execute("http://10.0.2.2:3000/tasas/calendarioPagos/"+idEF+"/"+(auxmonto).replace('.', ',')+"/"+cuota+".json")).get();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
		try {
			for(int i = 0; i < jArray.length(); i++){
				 JSONObject jsonItem = jArray.getJSONObject(i);
		         JSONObject jsonItem1 = jsonItem.getJSONObject("cp");
		         
		         double amortizacion = jsonItem1.getDouble("amortizacion");
		         //String fecCreacion = jsonItem1.("fecCreacion");
		         //String fecActualizacion = jsonItem1.getString("fecActualizacion");
		         double cuota = jsonItem1.getDouble("cuota");
		         double deudafinal = jsonItem1.getDouble("deudafinal");
		         double deudaprincipal = jsonItem1.getDouble("deudaprincipal");
		         double interes = jsonItem1.getDouble("interes");
		         int periodo = jsonItem1.getInt("periodo");
		        		 
		         Periodo p = new Periodo(amortizacion, cuota, deudafinal, deudaprincipal, 
		     			interes, periodo);
		         calendario.add(p);
		        
			}
		}catch (JSONException e) {
		     e.printStackTrace();
		 }
		return calendario;
	}
	
	public void loadTableLayout(){
		
		//tl.removeAllViews();
		for (int intContador=0;intContador<calendario.size();intContador++){
			TableRow tblrDetalle = new TableRow(this);			
			tblrDetalle.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			
			TextView txtPeriodo = new TextView(this);
			txtPeriodo.setText(calendario.get(intContador).getPeriodo()+"");
			txtPeriodo.setLayoutParams(new LayoutParams(((TextView) findViewById(R.id.CPCol1)).getWidth(),android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			txtPeriodo.setGravity(Gravity.CENTER);
			tblrDetalle.addView(txtPeriodo);
			
			TextView txtPrincipal = new TextView(this);
			txtPrincipal.setText(calendario.get(intContador).getDeudaprincipal()+"");
			txtPrincipal.setLayoutParams(new LayoutParams(((TextView) findViewById(R.id.CPCol2)).getWidth(),android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			txtPrincipal.setGravity(Gravity.CENTER);
			tblrDetalle.addView(txtPrincipal);			
			
			TextView txtAmortizacion = new TextView(this);
			txtAmortizacion.setText(calendario.get(intContador).getAmortizacion()+"");
			txtAmortizacion.setLayoutParams(new LayoutParams(((TextView) findViewById(R.id.CPCol3)).getWidth(),android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			txtAmortizacion.setGravity(Gravity.CENTER);
			tblrDetalle.addView(txtAmortizacion);
			
			TextView txtInteres = new TextView(this);
			txtInteres.setText(calendario.get(intContador).getInteres()+"");
			txtInteres.setLayoutParams(new LayoutParams(((TextView) findViewById(R.id.CPCol4)).getWidth(),android.view.ViewGroup.LayoutParams.WRAP_CONTENT));
			txtInteres.setGravity(Gravity.CENTER);
			tblrDetalle.addView(txtInteres);
			
			tl.addView(tblrDetalle);
			
			
		}		
		//http://www.warriorpoint.com/blog/2009/07/01/android-creating-tablerow-rows-inside-a-tablelayout-programatically/
	}
}
